package it.fantacalcio.struts.action.amministrazione.partita;

import java.util.ArrayList;
import java.util.List;

import it.fantacalcio.model.Partita;
import it.fantacalcio.model.Squadra;
import it.fantacalcio.model.User;
import it.fantacalcio.spring.service.MyServiceFactory;
import it.fantacalcio.struts.form.PartitaForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

public class PrepareCRUDPartitaAction extends DispatchAction {
	
	@Override
	protected ActionForward dispatchMethod(ActionMapping arg0, ActionForm arg1,
			HttpServletRequest arg2, HttpServletResponse arg3, String arg4)
			throws Exception {
		
		User userInSession = (User) arg2.getSession().getAttribute("userInSession");
		if(userInSession==null || userInSession.getAttivo().equals("NO")){
			return arg0.findForward("noninsession");
		}
		return super.dispatchMethod(arg0, arg1, arg2, arg3, arg4);
	}
	
	public ActionForward aggiungi(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		User userInSession = (User) request.getSession().getAttribute("userInSession");
		if(!userInSession.getRole().getCodRole().equals("ADMIN")){
			return mapping.findForward("noninsession");
		}
		
		List<Squadra> listaSquadra = new ArrayList<Squadra>();
		try {
			listaSquadra = MyServiceFactory.getSquadraService().list();
		} catch (Exception e) {
			e.printStackTrace();
			return mapping.findForward("error");
		}
		request.setAttribute("listaSquadra", listaSquadra);
		return mapping.findForward("aggiungi");
	}
	
	public ActionForward modifica(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		User userInSession = (User) request.getSession().getAttribute("userInSession");
		if(!userInSession.getRole().getCodRole().equals("ADMIN")){
			return mapping.findForward("noninsession");
		}
		
		PartitaForm partitaForm=(PartitaForm)form;
		Long idPartita = Long.parseLong(request.getParameter("id"));
		Partita partita = new Partita();
		List<Squadra> listaSquadra = new ArrayList<Squadra>();
		try {
			partita = MyServiceFactory.getPartitaService().get(idPartita);
			BeanUtils.copyProperties(partitaForm, partita);
			if(partita.getSquadraInCasa()!=null){
				partitaForm.setIdSquadraInCasa(partita.getSquadraInCasa().getId());
			}
			if(partita.getSquadraFuoriCasa()!=null){
				partitaForm.setIdSquadraFuoriCasa(partita.getSquadraFuoriCasa().getId());
			}
			listaSquadra = MyServiceFactory.getSquadraService().list();
		} catch (Exception e) {
			e.printStackTrace();
			return mapping.findForward("error");
		}
		request.setAttribute("listaSquadra", listaSquadra);
		return mapping.findForward("modifica");
	}
	
	public ActionForward cancella(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		User userInSession = (User) request.getSession().getAttribute("userInSession");
		if(!userInSession.getRole().getCodRole().equals("ADMIN")){
			return mapping.findForward("noninsession");
		}
		
		Long idPartita = Long.parseLong(request.getParameter("id"));
		Partita partita = new Partita();
		try {
			partita = MyServiceFactory.getPartitaService().get(idPartita);
		} catch (Exception e) {
			e.printStackTrace();
			return mapping.findForward("error");
		}
		request.setAttribute("partita", partita);
		return mapping.findForward("cancella");
	}
	
	public ActionForward apri(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		Long idPartita = Long.parseLong(request.getParameter("id"));
		Partita partita = new Partita();
		try {
			partita = MyServiceFactory.getPartitaService().get(idPartita);
		} catch (Exception e) {
			e.printStackTrace();
			return mapping.findForward("error");
		}
		request.setAttribute("partita", partita);
		return mapping.findForward("apri");
	}

}
